package uit.jee.lib.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import uit.jee.lib.access.mapper.CommentMapper;
import uit.jee.lib.bo.BookBO;
import uit.jee.lib.dto.BookDTO;
import uit.jee.lib.dto.CommentDTO;

/**
 *
 * @author ngloc_it
 */
@WebServlet(name="BookDetail", urlPatterns={"/BookDetail"})
public class BookDetail extends HttpServlet {
   
   // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        HttpSession session = req.getSession();

        String function = req.getParameter("function");
        String bookId = req.getParameter("bookid");
        try {
            // Load data here
            BookDTO book = getBook(bookId);
            if(book != null){
                session.setAttribute("book", book);
                ArrayList<CommentDTO> comments = getComment(bookId);
                session.setAttribute("comments", comments);                
            }
        } catch (Exception ex) {
            Logger.getLogger(BookDetail.class.getName()).log(Level.SEVERE, null, ex);
        }

        String path = "./jsps/bookdetail.jsp";
        resp.sendRedirect(path);
    }

    private BookDTO getBook(String bookId) throws Exception{
        BookBO bookBo = new BookBO();
        return bookBo.getBookById(bookId);
    }

    private ArrayList<CommentDTO> getComment(String bookId) throws Exception{
        CommentMapper commentMapperr = new CommentMapper();
        return commentMapperr.GetCommentOnBook(bookId);
    }
  
}
